#!/usr/bin/python
# -*- coding: utf-8 -*-

#twitter-utils version 0.1
#Programed by Takuji Tokiwa (tokiwatch@gmail.com).
#Published under New BSD License.

import os
import ConfigParser
import twitter
from optparse import OptionParser

###########################################################################################################

#Import Config file and Setup

CONFIG_FILE = os.getenv('HOME') + os.sep + '.twitrc'

conf = ConfigParser.SafeConfigParser()
conf.read(CONFIG_FILE)

USERNAME = conf.get('twitter_account', 'username')
PASSWORD = conf.get('twitter_account', 'password')
ENCODE = conf.get('terminal_setup','encode') 

###########################################################################################################

#set up command line options

usage = 'usage: %prog [options]'
version='%prog 0.1'
parser = OptionParser(usage=usage, version=version)
parser.add_option('-u', '--user', action='store', type='string', dest='screen_name', default='', 
                  help='Specifies the ID or screen name of the user for whom to return the friends_timeline.')
parser.add_option('-c', '--count', action='store', type='int', dest='count',
                  help='The number of status messages to retrieve. This option works with "-u" option')
parser.add_option('-s', '--since', action='store', type='string', dest='since',
                  help='Narrows the returned results to just those statuses created after the specified HTTP-formatted date. This option require "-u" and "-s" options')
parser.add_option('-e', '--encode', action='store', dest='ENCODE',
                  help='Encode [utf-8 | euc-jp ...]') 
parser.add_option('-d', '--dump', action='store_true', dest='dump',
                  help='Dump mode (tab separation)')
parser.add_option('-a', '--absolute', action='store_true', dest='absolute_mode',
                  help='Show post date by absolute date')

#analyse options
#imported options are stored as 'options' instance

(options, args) = parser.parse_args()

###########################################################################################################

#Fetch Twitter


api = twitter.Api(USERNAME, PASSWORD)

if options.screen_name:
	twitter_data = api.GetUserTimeline(options.screen_name, options.count, options.since)
else:
	twitter_data = api.GetFriendsTimeline(options.screen_name, options.since)

#store each

list_user = [s.user.screen_name.encode(ENCODE) for s in twitter_data]
list_text = [s.text.encode(ENCODE) for s in twitter_data]

if options.absolute_mode:
	list_date = [s.created_at.encode(ENCODE) for s in twitter_data]
else:
	list_date = [s.relative_created_at.encode(ENCODE) for s in twitter_data]

#output
if options.dump:
	for counter in range(len(list_user)):
		print list_user[counter] + '\t' + list_text[counter] +'\t' + list_date[counter]
else:
	max_user_name_length = max([len(user_name) for user_name in list_user]) 	#check longest screen_name and store value
	for counter in range(len(list_user)):
		print list_user[counter].ljust(max_user_name_length ) + '\t' + list_text[counter] + '\t(' + list_date[counter] + ')'


